﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using context;
using entities;

namespace DataAccessLibrary.DAL
{
    public class CheckinDAO
    {
        public List<CheckIn> SelectCheckInsByBookingId(int bookingId)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.CheckIns.Where(p => p.BookingID == bookingId && p.CheckOutEmployee == null ).ToList();
            }
        }

        public List<CheckIn> SelectCheckInsByCustomerId(int customerId)
        {
            using (var ctx = new BlueMDataContext())
            {
                return
                    ctx.CheckIns.Where(p => p.Booking.CustomerID == customerId && p.CheckInDate > DateTime.Today && p.CheckOutEmployee == null).ToList
                        ();
            }
        }

        public bool DeleteCheckIn(int checkInId)
        {
            using (var ctx = new BlueMDataContext())
            {
                CheckIn checkIn = ctx.CheckIns.First(p => p.CheckInID == checkInId);
                ctx.CheckIns.DeleteOnSubmit(checkIn);
                if (checkIn.CheckInEmployee>0||checkIn.Available)
                {
                    throw new Exception("Can't delete avalable checkin");
                }
                if (checkIn.Booking.CheckIns.Count == 0)
                {
                    ctx.Bookings.DeleteOnSubmit(checkIn.Booking);
                }
                ctx.SubmitChanges();
            }

            return true;
        }

        public CheckIn SelectCheckIn(int checkinId)
        {
            using (var ctx = new BlueMDataContext())
            {
                return ctx.CheckIns.First(p => p.CheckInID == checkinId && p.CheckOutEmployee == null);
            }
        }

        public List<CheckIn> SelectCheckInsIncome()
        {
            using (var ctx = new BlueMDataContext())
            {
                return
                    ctx.CheckIns.Where(p => p.CheckInDate > DateTime.Today && p.CheckOutEmployee == null).OrderByDescending(p => p.CheckInDate).ToList
                        ();
            }
        }

        public bool LockCheckin(List<int> checkinIds)
        {
            using (var ctx = new BlueMDataContext())
            {
                foreach (int i in checkinIds)
                {
                    CheckIn query = ctx.CheckIns.First(p => p.CheckInID == i);
                    if (query.Available)
                    {
                        return true;
                    }
                    query.Available = true;
                }
                ctx.SubmitChanges();
            }

            return true;
        }

        public List<vSupplyOrder> SelectSupplyOrders(int checkinId)
        {
            using (var ctx = new BlueMDataContext())
            {

     IQueryable<CheckIn> xs = ctx.Bookings.Where(p => p.CustomerID == 7).SelectMany(p => p.CheckIns);
                return ctx.vSupplyOrders.Where(p => p.CheckInID == checkinId).ToList();


            }
        }

        public bool AcceptCheckIn(int checkinId, int employeeId, string guestInfo)
        {
            using (var ctx = new BlueMDataContext())
            {
                CheckIn checkin = ctx.CheckIns.First(p => p.CheckInID == checkinId);
                TimeSpan ct;
                var now = DateTime.Now;
                if (now > checkin.CheckInDate)
                {
                    ct = DateTime.Now - checkin.CheckInDate;
                    if (ct > new TimeSpan(12, 0, 0)) throw new Exception("Checkin is out of date");
                }
                else
                {
                    ct = checkin.CheckInDate - now;
                    if (ct < new TimeSpan(6, 0, 0)) throw new Exception("Checkin is too premature");
                }
                //---------------- Check in bị khóa trong thời gian từ 24h đến 5h---------
                //if (ct > new TimeSpan(12, 0, 0)) throw new Exception("Checkin is out of date");
                //if (ct < new TimeSpan(6, 0, 0)) throw new Exception("Checkin is too premature");
                //------------------------------------------------------------------------
                if (checkin.CheckInEmployee != null) throw new Exception("Invalid checkin list");
                checkin.CheckInEmployee = employeeId;
                checkin.GuestInfo = guestInfo;
                ctx.SubmitChanges();
            }

            return true;
        }

        public bool UpdateCheckIn(CheckIn checkIn)
        {
            using (var ctx = new BlueMDataContext())
            {
                CheckIn oCheckIn = ctx.CheckIns.First(p => p.CheckInID == checkIn.CheckInID);
                oCheckIn.GuestInfo = checkIn.GuestInfo;
                ctx.SubmitChanges();
            }

            return true;
        }
    }
}